package cn.iune.web.erp.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.iune.config.SecurityService;
import cn.iune.erp.provider.FileProvider;
import cn.iune.erp.provider.IFileProviderGroupService;
import cn.iune.erp.provider.IFileProviderService;



@Controller
@RequestMapping(value="/provider")
public class ProviderController {
	
	@Autowired
	private IFileProviderService fileProviderService;
	
	@Autowired
	private IFileProviderGroupService fileGroupService;
	
	
	@RequestMapping(value="/providerInfo/single",method=RequestMethod.GET)
	public ModelAndView providerSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/provider/providerInfo/single");
		return mav;
	}

	@RequestMapping(value={
			"/providerInfo/single/query"
//			,"/storageRecord/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return fileProviderService.queryData(data);
	}

	@RequestMapping(value="/providerInfo/single/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerSave(@RequestBody JSONObject object) {
		return fileProviderService.saveData(object);
	}

	@RequestMapping(value="/providerInfo/single/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerDelete(@RequestBody JSONObject data) {
		return fileProviderService.deleteData(data);
	}

	@RequestMapping(value={
			"/providerInfo/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = fileProviderService.newData(data);
		return json;
	}

	@RequestMapping(value="/providerInfo/single/queryDetail",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerQueryDetail(@RequestBody JSONObject data) {
		return fileProviderService.queryDataDetail(data);
	}

	@RequestMapping(value="/providerInfo/single/createDetail",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerCreateDetail(@RequestBody JSONObject data) {
		JSONObject json = fileProviderService.newDataDetail(data);
		return json;
	}
	
	
	
	

	
	@RequestMapping(value="/providerInfo/selectSingle",method=RequestMethod.GET)
	public ModelAndView providerSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/provider/providerInfo/selectSingle");
		return mav;
	}

	@RequestMapping(value={"/providerInfo/selectSingle/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerQueryUNION(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return fileProviderService.queryUNION(data);
	}

	//根据客户编码获取客户信息
	@RequestMapping(value={"/getProviderByNo"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getProviderByNo(@RequestBody JSONObject data){
		String providerNo = data.getString("customerNo");
//		String enterpriseNo = data.getString("enterpriseNo");
		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		JSONObject json = new JSONObject();
		FileProvider fileProvider = fileProviderService.getByNo(providerNo, enterpriseNo);
		
		if(fileProvider==null){
			//客户不存在
			json.put("code", -1);
			json.put("status", "编码为：‘"+providerNo+"’的客户不存在");
		}else{
			//客户存在
			json.put("code", 0);
			json.put("status", "操作成功");
			json.put("data", fileProvider);
		}
		
		/*
		List<FileProvider> fileProviders = fileProviderService.getByNo(providerNo, enterpriseNo);
		if(fileProviders.size()<=0){
			//客户不存在
			json.put("code", -1);
			json.put("status", "编码为：‘"+providerNo+"’的客户不存在");
		}else if(fileProviders.size()>1){
			json.put("code", 1);
			json.put("status", "编码为：‘"+providerNo+"’的客户多于1个");
			json.put("count", fileProviders.size());
		}else{
			//客户存在
			json.put("code", 0);
			json.put("status", "操作成功");
			json.put("data", fileProviders.get(0));
		}
		*/

		return json;
	}

	
	
	
	
	
	//分类
	@RequestMapping(value="/providerGroup/tree",method=RequestMethod.GET)
	public ModelAndView providerGroupTreeView(){
		ModelAndView mav = new ModelAndView("/appcenter/provider/providerGroup/tree");
		return mav;
	}
	
	//重新排序
	@RequestMapping(value="/providerGroup/sortView",method=RequestMethod.GET)
	public ModelAndView providerGroupSortView(){
		ModelAndView mav = new ModelAndView("/appcenter/provider/providerGroup/sort");
		return mav;
	}
	
	//移动
	@RequestMapping(value="/providerGroup/moveView",method=RequestMethod.GET)
	public ModelAndView providerGroupMoveView(){
		ModelAndView mav = new ModelAndView("/appcenter/provider/providerGroup/move");
		return mav;
	}
	
	//选择物料分类（单选）
	@RequestMapping(value="/providerGroup/selectSingle",method=RequestMethod.GET)
	public ModelAndView providerGroupSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/provider/providerGroup/selectSingle");
		return mav;
	}
	
	@RequestMapping(value={
			"/providerGroup/tree/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerGroupCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = fileGroupService.newData(data);
		return json;
	}

	@RequestMapping(value= {
			"/providerGroup/tree/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerGroupQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return fileGroupService.queryData(data);
	}

	@RequestMapping(value= {
			"/providerGroup/tree/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerGroupSave(@RequestBody JSONObject object) {
		return fileGroupService.saveData(object);
	}
	
	@RequestMapping(value= {
			"/providerGroup/tree/delete"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject providerGroupDelete(@RequestBody JSONObject data) {
		return fileGroupService.deleteData(data);
	}

	
	@RequestMapping(value= {
			"/providerGroup/tree/sort"
			},method=RequestMethod.POST)
	@ResponseBody
	public void providerGroupSort(@RequestBody JSONObject data){
		String parentID = data.getString("parentID");
		JSONArray ids =  data.getJSONArray("ids");
		fileGroupService.sort(parentID, ids);
	}
	
	@RequestMapping(value= {
			"/providerGroup/tree/move"
			},method=RequestMethod.POST)
	@ResponseBody
	public void providerGroupMove(@RequestBody JSONObject data){
		String rowID = data.getString("rowID");
		String oldParentID = data.getString("oldParentID");
		String newParentID = data.getString("newParentID");
		fileGroupService.move(rowID, oldParentID, newParentID);
	}
	

	
	

}
